package com.camel.base.ei;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.stream.Collectors;

public class FileUtils {

    public static final Logger logger = LoggerFactory.getLogger(FileUtils.class);

    /**
     * 读取问目录下json文件
     * @param fileName 文件名
     * @return
     */
    public static JSONObject readJsonFile(String fileName) {
        return readJsonFile("mockData", fileName);
    }

    /**
     * 读取问目录下json文件
     * @param dirName 目录名
     * @param fileName 文件名
     * @return
     */
    public static JSONObject readJsonFile(String dirName, String fileName) {
        JSONObject object = null;
        try {
            InputStream inputStream = new ClassPathResource(dirName + "/" + fileName).getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String str = bufferedReader.lines().collect(Collectors.joining(System.lineSeparator()));
            object = JSONObject.parseObject(str);
        } catch (IOException e) {
            logger.error(e.getMessage());
        }
        return object;
    }
}
